<script setup lang='ts'>
  import { onMounted, reactive } from 'vue';
  import { guild_roomApi } from '@/api/controller';
  import { ToastWarning, Toastloading } from '@/utils/public';
  import { getPlayEndTime, getDateArray, getSummaries } from '@/utils'
  import Pagination from "@/components/Pagination/index.vue";
  import xmDatePicker from '@/components/xmDatePicker/index.vue';
  import table2excel from 'js-table2excel';

  const action = reactive({
    total:0,
    list:[] as any[],
    timeObj:getPlayEndTime(getDateArray('ymd'))
  })
  const listQuery = reactive<any>({
    page:1,
    limit:20,
    user_id:'',
    union_id: '',
  })
  const handleDownload = async()=>{
    const loading = Toastloading('数据导出中...')
    const { playTime:date_st, endTime:date_ed } = action.timeObj
    const res = await guild_roomApi.getIdentityMsgStatisticsList({
      ...listQuery,
      limit:action.total || 20,
      date_st,
      date_ed
    })
    const { data=[]} = res.data
    loading.close()
    if(data.length == 0) {
      ToastWarning('暂无可导出数据')
      return
    }
    const tHeader = [
      { title: '用户ID', key: "from_user_id"},
      { title: '主播UUID', key: "uuid"},
      { title: '主播昵称', key: "nickname"},
      { title: '公会ID', key: "union_id"},
      { title: '公会名称', key: "union_name"},
      { title: '私信总条数', key: "sms_num"},
      { title: '私信总人数', key: "user_num"},
      { title: '私信新用户总数', key: "new_use_num"},
      { title: '私信新用户总次数', key: "new_sms_num"},
      { title: '最早私信时间', key: "created_at"},
      { title: '最晚私信时间', key: "updated_at"},
    ]
    table2excel(tHeader, data, '主播私信数量统计')
  }
  const handleSerach = ()=>{
    listQuery.page = 1
    getList()
  }
  const getList = async()=> {
    const loading = Toastloading()
    const { playTime:date_st, endTime:date_ed } = action.timeObj
    const res = await guild_roomApi.getIdentityMsgStatisticsList({
      ...listQuery,
      date_st,
      date_ed
    })
    loading.close()
    const { data=[], total=0 } = res.data
    action.total = total
    action.list = data
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">公会ID：</div>
          <el-input
            v-model="listQuery.union_id"
            placeholder="请输入公会ID"
          />
        </div>
        <div class="labelItem">
          <div class="label">用户ID：</div>
          <el-input
            v-model="listQuery.user_id"
            placeholder="请输入用户ID"
          />
        </div>
        <xmDatePicker type="daterange" v-model:timeval="action.timeObj" />
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="warning" @click="handleDownload">导出</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border>
      <el-table-column width="120" prop="from_user_id" label="用户ID" />
      <el-table-column width="120" prop="uuid" label="主播UUID" />
      <el-table-column min-width="180" show-overflow-tooltip prop="nickname" label="主播昵称" />
      <el-table-column width="120" prop="union_id" label="公会ID" />
      <el-table-column min-width="180" show-overflow-tooltip prop="union_name" label="公会名称" />
      <el-table-column width="140" prop="sms_num" label="私信总条数" />
      <el-table-column width="140" prop="user_num" label="私信总人数" />
      <el-table-column width="140" prop="new_use_num" label="私信新用户总数" />
      <el-table-column width="140" prop="new_sms_num" label="私信新用户总次数" />
      <el-table-column width="140" prop="created_at" label="最早私信时间" />
      <el-table-column width="140" prop="updated_at" label="最晚私信时间 " />
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
  </div>
</template>